﻿namespace MVCSample.UI.Extensions
{
    using System.Collections.Generic;
    using Entities.Blogs;
    using Entities.Books;
    using Entities.Images;
    using Entities.Locals;
    using Entities.Patents;
    using Entities.Videos;
    using Entities.Webs;
    using Models;
    using BlogContract = Cik.Gapi4Net.Domain.Output.BlogSearch.Contract;
    using BlogEntity = Cik.Gapi4Net.Domain.Output.BlogSearch;
    using BookContract = Cik.Gapi4Net.Domain.Output.BookSearch.Contract;
    using BookEntity = Cik.Gapi4Net.Domain.Output.BookSearch;
    using ImageContract = Cik.Gapi4Net.Domain.Output.ImageSearch.Contract;
    using ImageEntity = Cik.Gapi4Net.Domain.Output.ImageSearch;
    using LocalContract = Cik.Gapi4Net.Domain.Output.LocalSearch.Contract;
    using LocalEntity = Cik.Gapi4Net.Domain.Output.LocalSearch;
    using Model = Entities;
    using NewsContract = Cik.Gapi4Net.Domain.Output.NewsSearch.Contract;
    using NewsEntity = Cik.Gapi4Net.Domain.Output.NewsSearch;
    using PatentContract = Cik.Gapi4Net.Domain.Output.PatentSearch.Contract;
    using PatentEntity = Cik.Gapi4Net.Domain.Output.PatentSearch;
    using VideoContract = Cik.Gapi4Net.Domain.Output.VideoSearch.Contract;
    using VideoEntity = Cik.Gapi4Net.Domain.Output.VideoSearch;
    using WebContract = Cik.Gapi4Net.Domain.Output.WebSearch.Contract;
    using WebEntity = Cik.Gapi4Net.Domain.Output.WebSearch;

    public static class Mapping
    {
        public static NewsSearchModel MappingNewsResult(this NewsEntity.NewsSearchResult inputResult)
        {
            var model = new NewsSearchModel();

            model.ListOfNews = new List<Model.News.News>();

            if (inputResult.ResponseData != null)
            {
                foreach (var result in inputResult.ResponseData.MainResult)
                {
                    var news = NewsMapping(result);
                    if (result.Image != null)
                    {
                        news.Image = ImageMapping(result.Image);
                    }
                    if (result.RelatedStories != null && result.RelatedStories.Length > 0)
                    {
                        news.Stories = new List<Model.News.Story>();

                        foreach (var story in result.RelatedStories)
                        {
                            news.Stories.Add(StoryMapping(story));
                        }

                        model.ListOfNews.Add(news);
                    }
                }
            }

            return model;
        }

        public static WebSearchModel MappingWebResult(this WebEntity.WebSearchResult inputResult)
        {
            var model = new WebSearchModel();

            model.ListOfWebs = new List<Web>();

            foreach (var web in inputResult.ResponseData.MainResult)
            {
                model.ListOfWebs.Add(WebMapping(web));
            }

            return model;
        }

        public static BlogSearchModel MappingBlogResult(this BlogEntity.BlogSearchResult inputResult)
        {
            var model = new BlogSearchModel();

            model.ListOfBlogs = new List<Blog>();

            foreach (var blog in inputResult.ResponseData.MainResult)
            {
                model.ListOfBlogs.Add(BlogMapping(blog));
            }

            return model;
        }

        public static BookSearchModel MappingBookResult(this BookEntity.BookSearchResult inputResult)
        {
            var model = new BookSearchModel();

            model.ListOfBooks = new List<Book>();

            foreach (var book in inputResult.ResponseData.MainResult)
            {
                model.ListOfBooks.Add(BookMapping(book));
            }

            return model;
        }

        public static ImageSearchModel MappingImageResult(this ImageEntity.ImageSearchResult inputResult)
        {
            var model = new ImageSearchModel();

            model.ListOfImages = new List<Image>();

            foreach (var image in inputResult.ResponseData.MainResult)
            {
                model.ListOfImages.Add(ImageMapping(image));
            }

            return model;
        }

        public static LocalSearchModel MappingLocalResult(this LocalEntity.LocalSearchResult inputResult)
        {
            var model = new LocalSearchModel();

            model.ListOfLocals = new List<Local>();

            foreach (var local in inputResult.ResponseData.MainResult)
            {
                model.ListOfLocals.Add(LocalMapping(local));
            }

            return model;
        }

        public static PatentSearchModel MappingPatentResult(this PatentEntity.PatentSearchResult inputResult)
        {
            var model = new PatentSearchModel();

            model.ListOfPatents = new List<Patent>();

            foreach (var local in inputResult.ResponseData.MainResult)
            {
                model.ListOfPatents.Add(PatentMapping(local));
            }

            return model;
        }

        public static VideoSearchModel MappingVideoResult(this VideoEntity.VideoSearchResult inputResult)
        {
            var model = new VideoSearchModel();
            model.ListOfVideos = new List<Video>();

            foreach (var video in inputResult.ResponseData.MainResult)
            {
                model.ListOfVideos.Add(VideoMapping(video));
            }

            return model;
        }

        #region private function

        private static Model.News.News NewsMapping(NewsContract.IMainResult mainResult)
        {
            var news = new Model.News.News
            {
                ClusterUrl = mainResult.ClusterUrl,
                Content = mainResult.Content,
                GsearchResultClass = mainResult.GsearchResultClass,
                Language = mainResult.Language,
                Location = mainResult.Location,
                PublishedDate = mainResult.PublishedDate,
                Publisher = mainResult.Publisher
            };

            return news;
        }

        private static Model.News.Image ImageMapping(NewsContract.IImage img)
        {
            var image = new Model.News.Image
            {
                OriginalContextUrl = img.OriginalContextUrl,
                Publisher = img.Publisher,
                Url = img.Url,
                Width = img.TbWidth,
                Height = img.TbHeight
            };

            return image;
        }

        private static Model.News.Story StoryMapping(NewsContract.IRelatedStories relatedStory)
        {
            var story = new Model.News.Story
            {
                Language = relatedStory.Language,
                Location = relatedStory.Location,
                PublishedDate = relatedStory.PublishedDate,
                Publisher = relatedStory.Publisher,
                SignedRedirectUrl = relatedStory.SignedRedirectUrl,
                Title = relatedStory.Title,
                TitleNoFormatting = relatedStory.TitleNoFormatting,
                UnescapedUrl = relatedStory.UnescapedUrl,
                Url = relatedStory.Url
            };

            return story;
        }

        private static Web WebMapping(WebContract.IMainResult mainResult)
        {
            var web = new Web
            {
                CacheUrl = mainResult.CacheUrl,
                Content = mainResult.Content,
                GsearchResultClass = mainResult.GsearchResultClass,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting,
                UnescapedUrl = mainResult.UnescapedUrl,
                Url = mainResult.Url,
                VisibleUrl = mainResult.VisibleUrl
            };

            return web;
        }

        private static Blog BlogMapping(BlogContract.IMainResult mainResult)
        {
            var blog = new Blog
            {
                Author = mainResult.Author,
                BlogUrl = mainResult.BlogUrl,
                Content = mainResult.Content,
                GsearchResultClass = mainResult.GsearchResultClass,
                PostUrl = mainResult.PostUrl,
                PublishedDate = mainResult.PublishedDate,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting
            };

            return blog;
        }

        private static Book BookMapping(BookContract.IMainResult mainResult)
        {
            var book = new Book
            {
                Authors = mainResult.Authors,
                BookId = mainResult.BookId,
                GsearchResultClass = mainResult.GsearchResultClass,
                PageCount = mainResult.PageCount,
                PublishedYear = mainResult.PublishedYear,
                TbHeight = mainResult.TbHeight,
                TbUrl = mainResult.TbUrl,
                TbWidth = mainResult.TbWidth,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting,
                UnescapedUrl = mainResult.UnescapedUrl,
                Url = mainResult.Url
            };

            return book;
        }

        private static Image ImageMapping(ImageContract.IMainResult mainResult)
        {
            var img = new Image
            {
                Content = mainResult.Content,
                ContentNoFormatting = mainResult.ContentNoFormatting,
                GsearchResultClass = mainResult.GsearchResultClass,
                Height = mainResult.Height,
                ImageId = mainResult.ImageId,
                OriginalContextUrl = mainResult.OriginalContextUrl,
                TbHeight = mainResult.TbHeight,
                TbUrl = mainResult.TbUrl,
                TbWidth = mainResult.TbWidth,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting,
                UnescapedUrl = mainResult.UnescapedUrl,
                Url = mainResult.Url,
                VisibleUrl = mainResult.Url,
                Width = mainResult.Width
            };

            return img;
        }

        private static Local LocalMapping(LocalContract.IMainResult mainResult)
        {
            var local = new Local
            {
                AddressLines = mainResult.AddressLines,
                City = mainResult.City,
                Content = mainResult.Content,
                Country = mainResult.Country,
                Lat = mainResult.Lat,
                Lng = mainResult.Lng,
                Region = mainResult.Region,
                StaticMapUrl = mainResult.StaticMapUrl,
                StreetAddress = mainResult.StreetAddress,
                Title = mainResult.Title,
                Url = mainResult.Url
            };

            return local;
        }

        private static Patent PatentMapping(PatentContract.IMainResult mainResult)
        {
            var patent = new Patent
            {
                ApplicationDate = mainResult.ApplicationDate,
                Assignee = mainResult.Assignee,
                Content = mainResult.Content,
                PatentNumber = mainResult.PatentNumber,
                PatentStatus = mainResult.PatentStatus,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting,
                UnescapedUrl = mainResult.UnescapedUrl,
                Url = mainResult.Url
            };

            return patent;
        }

        private static Video VideoMapping(VideoContract.IMainResult mainResult)
        {
            var video = new Video
            {
                Content = mainResult.Content,
                Duration = mainResult.Duration,
                PlayUrl = mainResult.PlayUrl,
                Published = mainResult.Published,
                Publisher = mainResult.Publisher,
                Rating = mainResult.Rating,
                Title = mainResult.Title,
                TitleNoFormatting = mainResult.TitleNoFormatting,
                Url = mainResult.Url,
                VideoType = mainResult.VideoType
            };

            return video;
        }

        #endregion private function
    }
}